package com.gy.assistant.ui.chat.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;

import im.zego.zim.callback.ZIMGroupNameUpdatedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.ui.BaseActivity;

public class ZIMGroupNameUpdateActivity extends BaseActivity {

    private EditText groupInfoEditText;

    public static void actionStart(Activity activity, String groupID, String groupName) {
        Intent intent = new Intent(activity, ZIMGroupNameUpdateActivity.class);
        intent.putExtra("groupID", groupID);
        intent.putExtra("groupName", groupName);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_info_update);
        TextView groupInfoText = findViewById(R.id.group_info_text);
        TextView groupInfoTitle = findViewById(R.id.group_info_title);

        groupInfoText.setText(getString(R.string.group_name));
        groupInfoTitle.setText(getString(R.string.edit_group_name));

        groupInfoEditText = findViewById(R.id.group_info);
        groupInfoEditText.setText(getIntent().getStringExtra("groupName"));

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onUpdateGroupInfo(View view) {
        String info = groupInfoEditText.getText().toString();
        String groupID = getIntent().getStringExtra("groupID");
        SDKManager.sharedInstance().updateGroupName(info, groupID, new ZIMGroupNameUpdatedCallback() {
            @Override
            public void onGroupNameUpdated(String groupID, String groupName, ZIMError errorInfo) {
                
                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(ZIMGroupNameUpdateActivity.this, "Failed to update group name error: " + errorInfo.code.name(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(ZIMGroupNameUpdateActivity.this, "Update success", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        });
    }

}
